package com.ctshk.rpc.order.air.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.air.dto.AirOrderCollectionInfoDTO;
import com.ctshk.rpc.order.air.dto.AirOrderReceiptInfoDTO;
import com.ctshk.rpc.order.air.entity.*;
import com.ctshk.rpc.order.air.manager.AirOrderManager;
import com.ctshk.rpc.order.air.mapper.*;
import com.ctshk.rpc.order.air.req.AirOrderReceiptEditReq;
import com.ctshk.rpc.order.air.req.orderDetail.AirOrderFlightData;
import com.ctshk.rpc.order.air.req.orderDetail.AirOrderFlightPriceData;
import com.ctshk.rpc.order.air.service.IAirOrderReceiptService;
import com.ctshk.rpc.system.dto.SysDepartmentDTO;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysDepartmentService;
import com.ctshk.rpc.system.service.ISysUserService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 机票订单-收据表 服务实现类
 * </p>
 *
 * @author lihang
 * @since 2021-03-17
 */
@DubboService
public class AirOrderReceiptServiceImpl extends ServiceImpl<AirOrderReceiptMapper, AirOrderReceipt> implements IAirOrderReceiptService {

    @DubboReference
    private ISysUserService iSysUserService;

    @DubboReference
    private ISysDepartmentService iSysDepartmentService;

    @Autowired
    private  AirOrderReceiptMapper airOrderReceiptMapper;

    @Autowired
    private AirOrderMapper airOrderMapper;

    @Autowired
    private AirOrderTouristMapper airOrderTouristMapper;

    @Autowired
    private AirOrderCollectionMapper airOrderCollectionMapper;

    @Autowired
    private AirOrderFlightMapper airOrderFlightMapper;



    @Override
    public Result add(Long orderId, TokenUser user) {
        UserDTO userDTO = iSysUserService.loadUserByParam(user.getId(), 1);
        SysDepartmentDTO sysDepartmentDTO =  iSysDepartmentService.queryByUserId(user.getId());
        /**查询订单信息*/
        AirOrder airOrder = airOrderMapper.selectById(orderId);
        /**查询订单旅客信息*/
        QueryWrapper<AirOrderTourist> orderTouristQueryWrapper =new QueryWrapper<>();
        orderTouristQueryWrapper.lambda().eq(AirOrderTourist::getOrderId,orderId);
        List<AirOrderTourist> airOrderTourists = airOrderTouristMapper.selectList(orderTouristQueryWrapper);
        StringBuffer guestBuf = new StringBuffer();
        for (AirOrderTourist airOrderTourist : airOrderTourists) {
            guestBuf.append(","+airOrderTourist.getTouristName());
        }
        AirOrderReceipt airOrderReceipt = new AirOrderReceipt();
        Long id = IdWorker.getId();
        airOrderReceipt.setId(id);
        airOrderReceipt.setOrderId(orderId);
        airOrderReceipt.setBranch(sysDepartmentDTO.getDeptCode()+"-"+sysDepartmentDTO.getTitle());
        airOrderReceipt.setCreateId(user.getId());
        airOrderReceipt.setGmtModified(LocalDateTime.now());
        airOrderReceipt.setIsDeleted(IsDeletedCode.NO.getCode());
        airOrderReceipt.setModifiedId(user.getId());
        airOrderReceipt.setGmtModified(LocalDateTime.now());
        airOrderReceipt.setReceiptNo(AirOrderManager.getReceiptNumer(sysDepartmentDTO.getDeptCode(),userDTO.getEmployeeNumber()));
        airOrderReceipt.setLicenseNo(AirOrderManager.LICENSE_NO);
        airOrderReceipt.setSupportNo(AirOrderManager.SUPPORT_NO);
        airOrderReceipt.setGuest(guestBuf.substring(1));
        airOrderReceipt.setPhone(airOrder.getContactMobile());
        airOrderReceiptMapper.insert(airOrderReceipt);
        return Result.success(id);
    }

    @Override
    public Result edit(AirOrderReceiptEditReq req, TokenUser tokenUser) {
        UpdateWrapper<AirOrderReceipt> airOrderReceiptUpdateWrapper = new UpdateWrapper<>();
        airOrderReceiptUpdateWrapper.lambda().eq(AirOrderReceipt::getId,req.getId());
        airOrderReceiptUpdateWrapper.lambda().set(StringUtils.isNotBlank(req.getAttention()),AirOrderReceipt::getAttention,req.getAttention());
        airOrderReceiptUpdateWrapper.lambda().set(StringUtils.isNotBlank(req.getGuest()),AirOrderReceipt::getGuest,req.getGuest());
        airOrderReceiptUpdateWrapper.lambda().set(StringUtils.isNotBlank(req.getVoucherO()),AirOrderReceipt::getVoucherO,req.getVoucherO());
        airOrderReceiptUpdateWrapper.lambda().set(StringUtils.isNotBlank(req.getPhone()),AirOrderReceipt::getPhone,req.getPhone());
        airOrderReceiptUpdateWrapper.lambda().set(StringUtils.isNotBlank(req.getRemark()),AirOrderReceipt::getRemark,req.getRemark());
        airOrderReceiptUpdateWrapper.lambda().set(AirOrderReceipt::getGmtModified,LocalDateTime.now());
        airOrderReceiptUpdateWrapper.lambda().set(AirOrderReceipt::getModifiedId,tokenUser.getId());
        airOrderReceiptMapper.update(null,airOrderReceiptUpdateWrapper);
        return Result.success(req.getId());
    }

    @Override
    public Result<AirOrderReceiptInfoDTO> info(Long orderId) {
        /**查询订单信息*/
        AirOrder airOrder  = airOrderMapper.selectById(orderId);
        if(null==airOrder){
            return  Result.failed(SystemError.AIR_ORDER_15000);
        }
        QueryWrapper<AirOrderReceipt> airOrderReceiptQueryWrapper = new QueryWrapper<>();
        airOrderReceiptQueryWrapper.lambda().eq(ObjectUtils.isNotNull(orderId),AirOrderReceipt::getOrderId,orderId);
        AirOrderReceipt airOrderReceipt = airOrderReceiptMapper.selectOne(airOrderReceiptQueryWrapper);
        if(null==airOrderReceipt){
            return  Result.failed(SystemError.SYS_411);
        }
        /**查询收款信息*/
        QueryWrapper<AirOrderCollection> airOrderCollectionQueryWrapper = new QueryWrapper<>();
        airOrderCollectionQueryWrapper.lambda().eq(AirOrderCollection::getOrderId,airOrder.getId());
        List<AirOrderCollection> airOrderCollectionList  = airOrderCollectionMapper.selectList(airOrderCollectionQueryWrapper);
        /**查询航班信息*/
        QueryWrapper<AirOrderFlight> airOrderFlightQueryWrapper = new QueryWrapper<>();
        airOrderFlightQueryWrapper.lambda().eq(AirOrderFlight::getOrderId,airOrder.getId());
        List<AirOrderFlight> airOrderFlightList  = airOrderFlightMapper.selectList(airOrderFlightQueryWrapper);
        /**组装返回结果*/
        AirOrderReceiptInfoDTO airOrderReceiptInfoDTO = EntityUtil.copy(airOrderReceipt,AirOrderReceiptInfoDTO.class);
        airOrderReceiptInfoDTO.setOrderAmount(airOrder.getOrderAmount());
        List<AirOrderFlightData> airOrderFlightDataList = Lists.newArrayList();
        for (AirOrderFlight airOrderFlight : airOrderFlightList) {
            AirOrderFlightData airOrderFlightData = EntityUtil.copy(airOrderFlight,AirOrderFlightData.class);
            airOrderFlightData.setOrderFlightPriceDataList(JSON.parseArray(airOrderFlight.getFlightPrice(), AirOrderFlightPriceData.class));
            airOrderFlightDataList.add(airOrderFlightData);
        }
        airOrderReceiptInfoDTO.setAirOrderFlightDataList(airOrderFlightDataList);
        List<AirOrderCollectionInfoDTO> airOrderCollectionInfoDTOList = EntityUtil.copyList(airOrderCollectionList,AirOrderCollectionInfoDTO.class);
        airOrderReceiptInfoDTO.setAirOrderCollectionInfoDTOList(airOrderCollectionInfoDTOList);
        return Result.success(airOrderReceiptInfoDTO);
    }

}
